
*********************************************************************************
*Table 3: Individual fixed effects regression model on labor supply and work time
*********************************************************************************

version 15.1
cd "${mypath}\CHNS_project\01_data\02_posted\"

log using "${mypath}\CHNS_project\03_log_files\07_table_3_5.log", replace

use CHNS_1993_2015_20_imputed.dta,clear
mi import ice, automatic

*generate exclusion condition, that is, people who work in agriculture in urban area
gen be_farmer = (migrant_group != 2 & occupation == 3 & work_unit == 4) if be_employed == 1

*generate "over the statutory hours last week"
gen over_lastw = hours_lastw1 > 44 if employed_last1 == 1

*generate annual salary last year(log)
gen log_wage_i = log(exp(log_wage_1)*workmonths1+exp(log_bonuse_1)) if employed_last1 == 1

*generate hourly wage last year(log)
gen log_hour_wage = log_wage_1-log((365/12/7)*workdays1*workhours1) if employed_last1 == 1

*generate occupations
tab occupation if be_employed == 1 ,gen(occ)
la var occ2 "officer"
la val occ2 occ2
la var occ3 "farmer fisherman and hunter"
la val occ3 occ3
la var occ4 "skilled worker"
la val occ4 occ4
la var occ5 "non-skilled worker"
la val occ5 occ5
la var occ6 "service worker"
la val occ6 occ6
la var occ7 "other(including homemaker)"
la val occ7 occ7

*generate working positions
tab position if be_employed == 1 ,gen(posit)
la var posit2 "permanent worker"
la val posit2 posit2
la var posit3 "contractor"
la val posit3 posit3
la var posit4 "temporary worker"
la val posit4 posit4

*generate working units
gen unit2 = 1 if work_unit == 2 | work_unit == 3
replace unit2 = 0 if mi(unit2) & work_unit != 0
gen unit3 = 1 if work_unit == 4 | work_unit == 5
replace unit3 = 0 if mi(unit3) & work_unit != 0
la var unit2 "public enterprise"
la val unit2 unit2
la var unit3 "private enterprise"
la val unit3 unit3 

*assign outcome variables to the local macro
local outcome_1 "be_employed"
local outcome_2 "workmonths1"
local outcome_3 "hours_lastw1"
local outcome_4 "over_lastw"
local outcome_5 "log_wage_i"
local outcome_6 "log_hour_wage"

*assign key independent variables to the local macro
local inde "year1997 year2000 year2004 year2006 year2009 year2011 year2015 be_migrant be_migrant##year1997 be_migrant##year2000 be_migrant##year2004 be_migrant##year2006 be_migrant##year2009 be_migrant##year2011 be_migrant##year2015"

*assign control variables to the local macro
local control_1"age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 year_education marital_status h_group1 h_group2 h_group4 h_group5 num_child num_elderly population pcgdp child_elder percent_farmer service_sector"
local control_2 "age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 year_education marital_status h_group1 h_group2 h_group4 h_group5 occ2 occ3 occ4 occ5 occ6 occ7 num_child num_elderly population pcgdp child_elder percent_farmer service_sector"
local control_3 "age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 year_education marital_status h_group1 h_group2 h_group4 h_group5 occ2 occ3 occ4 occ5 occ6 occ7 posit2 posit3 posit4 unit2 unit3 num_child num_elderly population pcgdp child_elder percent_farmer service_sector"

local path "${mypath}\CHNS_project\04_tables\table_3_5\"

*predict inverse mills ratio
prob employed_last1 year1997 year2000 year2004 year2006 year2009 year2011 year2015 be_migrant be_migrant##year1997 be_migrant##year2000 be_migrant##year2004 be_migrant##year2006 be_migrant##year2009 be_migrant##year2011 be_migrant##year2015 age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 gender ethnicity year_education marital_status h_group1 h_group2 h_group4 h_group5 num_child num_elderly population pcgdp child_elder percent_farmer service_sector Liaoning Heilongjiang Shandong Henan Hubei Hunan Guangxi Guizhou if be_rural_resident == 0 & time != 1 & be_farmer !=  1
predict gw_urban, xb
gen lambda_urban=normalden(gw_urban)/normal(gw_urban) if employed_last1==1 & be_rural_resident == 0 & time != 1 & be_farmer !=  1

*declare imputated data as panel data
mi xtset IDind wave

*fixed-effect model for outcome1
quiet eststo: mi estimate,post esampvaryok: xtreg `outcome_1' `inde' `control_1' if be_rural_resident == 0 & time != 1 & be_farmer !=  1 & degree_education <= 3,fe r noomitted
est store outcome1,title(outcome1)

*fixed effect model + conrtol "inverse-mills ratio" for outcome2-6
forv i = 2/6 {
	forv j = 1/3 {
		quiet eststo: mi estimate,post esampvaryok: xtreg `outcome_`i'' `inde' `control_`j'' lambda_urban if employed_last1==1 & be_rural_resident == 0 & time != 1 & be_farmer !=  1 & degree_education <= 3,fe r noomitted
		est store outcome`i'_`j',title(outcome`i'_`j')
	}
}
esttab outcome1 outcome2_1 outcome3_1 outcome4_1 using "`path'02_table3.csv", constant b(3) star(+ 0.10 * 0.05 ** 0.01) se(%9.3f) r2 replace nogap nonumbers noomitted stats(N_mi)
esttab outcome5_1 outcome6_1 using "`path'03_table4.csv", constant b(3) star(+ 0.10 * 0.05 ** 0.01) se(%9.3f) r2 replace nogap nonumbers noomitted stats(N_mi)
esttab outcome2_2 outcome2_3 outcome3_2 outcome3_3 outcome4_2 outcome4_3 outcome5_2 outcome5_3 outcome6_2 outcome6_3 using "`path'04_table5.csv", constant b(3) star(+ 0.10 * 0.05 ** 0.01) se(%9.3f) r2 replace nogap nonumbers noomitted stats(N_mi)

log close
